//
//  M1VIPCenterVC.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/20.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1VIPCenterVC.h"
#import "M1MemberCell.h"
#import "M1MemberHeaderView.h"
#import "M1MemberModel.h"
#import "M1AppleIpa.h"
@interface M1VIPCenterVC ()

@property (nonatomic, strong) M1MemberHeaderView *m1_headerView;

@property (nonatomic, strong) UIButton *m1_commitBtn;
@property (nonatomic, strong) M1MemberModel *m1_vipModel;
@property (nonatomic, copy)   NSArray *m1_vipArray;
@end

@implementation M1VIPCenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"会员中心";
    
    self.m1_tableView.backgroundColor = [UIColor m1_colorHex:0xF6F8FB];

    [self.m1_tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(m1_NAV_HEIGHT, 20, 0, 20));
    }];
    
    self.m1_tableView.tableHeaderView = self.m1_headerView;
    
    UIView *footerView = [self viewForFooter];
    
    self.m1_tableView.tableFooterView = footerView;
    
    [self.m1_tableView registerClass:[M1MemberCell class] forCellReuseIdentifier:m1_tableViewCellIdentifier];
    
    [self m1_getVipPackage];
    
}

- (void)m1_commitAction:(UIButton *)sender{

    if (!_m1_vipModel) {
        [M1Alert m1_alertWithTitle:nil message:@"未知错误"];
        return;
    }
    NSString *m1_appid = _m1_vipModel.apple_id;
    m1_appid = [m1_appid stringByReplacingOccurrencesOfString:@"vip" withString:@"marry"];
    WEAKSELF;
    [[M1AppleIpa manager] m1_appIpaWithVC:self appleId:m1_appid block:^(id data) {
        NSDictionary *params = @{@"apple_id":weakSelf.m1_vipModel.apple_id,
                                 @"amount":weakSelf.m1_vipModel.package_apple_price,
                                 @"receipt":data
                                 };
        
        [M1HttpNet requestWithMethod:m1_payment_apple_pay_url params:params success:^(id resDic, NSInteger code, NSString *message) {
            M1User *user = [M1User user];
            user.status.member_fees_status = 1;
            [M1User saveModel:user];
            if (weakSelf.buyVip) {
                weakSelf.buyVip(YES);
            }
            [M1Alert m1_alertWithTitle:nil message:@"购买会员成功" buttonTitles:@[@"确定"] action:^(NSInteger index) {
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }];
        } fail:^(id resDic, NSInteger code, NSString *message) {
            [M1Alert m1_alertWithTitle:nil message:message];
        } showAni:YES];
    }];
    
}


-(void)m1_getVipPackage{
    WEAKSELF;
    [M1HttpNet requestWithMethod:m1_get_package_url params:@{@"package_type":@"17"} success:^(id resDic, NSInteger code, NSString *message) {
        weakSelf.m1_vipArray = [M1MemberModel mj_objectArrayWithKeyValuesArray:resDic];
        [weakSelf.m1_tableView reloadData];
        
        if (weakSelf.m1_vipArray.count >0) {
            weakSelf.m1_vipModel = weakSelf.m1_vipArray[0];
            [weakSelf.m1_commitBtn setTitle:[NSString stringWithFormat:@"购买%@",weakSelf.m1_vipModel.package_title] forState:0];
        }else{
            [M1Alert m1_alertWithTitle:nil message:@"未发现商品" buttonTitles:@[@"确定"] action:^(NSInteger index) {
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }];
        }
    } fail:^(id resDic, NSInteger code, NSString *message) {
        [M1Alert m1_alertWithTitle:nil message:message buttonTitles:@[@"确定"] action:^(NSInteger index) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    } showAni:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 125;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    M1MemberCell *cell = [tableView dequeueReusableCellWithIdentifier:m1_tableViewCellIdentifier];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.m1_vipArray = _m1_vipArray;
    WEAKSELF;
    cell.m1_vip = ^(M1MemberModel *vopModel) {
        weakSelf.m1_vipModel = vopModel;
        [weakSelf.m1_commitBtn setTitle:[NSString stringWithFormat:@"购买%@",weakSelf.m1_vipModel.package_title] forState:0];
    };
    return cell;
}

- (M1MemberHeaderView *)m1_headerView{
    if (!_m1_headerView) {
        CGFloat height = 60 * 2 + 65 * 4;
        _m1_headerView = [[M1MemberHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH - 40, height)];
    }
    return _m1_headerView;
}

- (UIView *)viewForFooter{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 94)];
    footerView.backgroundColor = [UIColor clearColor];
    
    self.m1_commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_m1_commitBtn setBackgroundImage:LOAD_LOCAL_IMG(@"personal_vip_commit") forState:UIControlStateNormal];
    [_m1_commitBtn setTitle:@"购买" forState:UIControlStateNormal];
    [_m1_commitBtn setTitleColor:m1_color(0xFFFFFF) forState:UIControlStateNormal];
    _m1_commitBtn.titleLabel.font = m1_font(18);
    [_m1_commitBtn addTarget:self action:@selector(m1_commitAction:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:_m1_commitBtn];
    
    [_m1_commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(footerView.mas_centerX);
        make.centerY.equalTo(footerView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(170, 55));
    }];
    
    return footerView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
